/*
 *  SymbolTable.cpp
 *  Compiler
 *
 *  Created by Jacopo De Stefani on 3/5/12.
 *  Copyright 2012 Politecnico di Milano. All rights reserved.
 *
 */

#include "ExtendedSymbolTable.h"

using namespace std;

//Adds a new entry in the table with default values and identifier id.
void ExtendedSymbolTable::addSymbolTableEntry(string identifier){
	symbolTable.insert(SymbolTableEntry(identifier,SymbolTableAttributes()));
}

//Adds a new entry in the table with default values and identifier id.
void ExtendedSymbolTable::addFunctionTableEntry(string identifier){
	queue<TypesP> paramList;
	functionTable.insert(FunctionTableEntry(identifier,paramList));
}

//Updates the attributes of a list of variables using currVarList.
void ExtendedSymbolTable::updateSymbolTable(TypesP type,ScopesP scope){
	SymbolTable::iterator currEl;
	while (!currVarList.empty()) {
		currEl = symbolTable.find(popVar());
		(currEl->second).setTypeP(type);
		(currEl->second).setScopeP(scope);
	}
}

//Updates the parameters' list for a given function using currTypeList;
void ExtendedSymbolTable::updateTypeList(string identifier){
	FunctionTable::iterator currEl;
	currEl = functionTable.find(identifier);
	//Eventually substitute it with deep copy - CHECK!!!
	(currEl->second) = currTypeList;
	
}

//Modifiers of the list of variables
void ExtendedSymbolTable::pushVar(string varId){
	currVarList.push(varId);
}
string ExtendedSymbolTable::popVar(void){
	string retVal = currVarList.front();
	currVarList.pop(); 
	return retVal;
}

void ExtendedSymbolTable::flushVarList(){
	while (!currVarList.empty()) {
		currVarList.pop();
	}
}


void ExtendedSymbolTable::concVarList(queue<string> first,queue<string> second){
	while (second.size() != 0) {
		first.push(second.front());
		second.pop();
	}
}



//Modifiers of the list of parameters
void ExtendedSymbolTable::pushType(TypesP paramType){
	currTypeList.push(paramType);
}

TypesP ExtendedSymbolTable::popType(void){
	TypesP retVal = currTypeList.front();
	currVarList.pop(); 
	return retVal;
}

void ExtendedSymbolTable::flushTypeList(){
	while (!currTypeList.empty()) {
		currTypeList.pop();
	}
}
						 
void ExtendedSymbolTable::concTypeList(queue<TypesP> first,queue<TypesP> second){
	while (second.size() != 0) {
		first.push(second.front());
		second.pop();
	}
}